<?php
/*
+ ---------------------------------------------------------------------- +
| e107Shop - An e-commerce plugin for the e107 content management system |
|                                                                        |
| ByOrder Europe 2007-2008                                               |
| http://e107shop.com                                                    |
| patrick@byorder.nl                                                     |
|                                                                        |
| Released under the terms and conditions of the LGPL                    |
|                                                                        |
| $Revision: 1.0                                                         |
| $Date: 2008/08/20                                                      |
| $Author: Bieleke                                                       |
+ ---------------------------------------------------------------------- +
*/

function e107shop_show_orders($display_type, $search) {
    global $e107shop_db_name, $sql, $ns;
    
    switch($display_type){
        Case 1:
            $query = "status = 'pending' Group By order_number order by order_number desc";
            break;
        Case 2:
            $query = "status = 'paid' Group By order_number order by order_number desc";
            break;
        Case 3:
            $query = "status = 'complete' Group By order_number order by order_number desc";
            break;
        Case 4:
            $query = $_POST["filter"]."='".$_POST["search_term"]."' Group by order_number";
            break;
        Case 5:
            $query = $search;
            break;
        case 6:
            $query = "1 order by last_mod desc";
            break;
    }

    if($query == "") $query = "lineID>0 Group By order_number order by order_number desc";
    
    $sql->db_Select($e107shop_db_name['orders'], "*", $query);
    $i=0;
    while($row=$sql->db_Fetch()) {
            $top_order[$i][0] = $row["lineID"];
            $top_order[$i][1] = $row["cart_ID"];
            $top_order[$i][2] = $row["productID"];
            $top_order[$i][3] = $row["quantity"];
            $top_order[$i][4] = $row["option"];
            $top_order[$i][5] = $row["order_number"];
            $top_order[$i][6] = $row["status"];
            $top_order[$i][7] = $row["last_mod"];
            $top_order[$i][8] = $row["gateway"];
            $top_order[$i][9] = $row["theuser"];
            $top_order[$i][10] = $row["cust_region"];
            $top_order[$i][11] = $row["coupon"];
            $top_order[$i][12] = $row["sale_price"];
            $i++;
    }
    return $top_order;
}

/*
    Function : e107shop_manage_orders
    -----------------------------------------------
    Parameters : $action
                 $order      The orderid
    ===============================================
    This function displays a box with a list
    of current products. A category ID can be sent
    to it to retrieve only items for that category.
    ===============================================
*/

function e107shop_manage_orders($order) {
    global $e107shop_db_name, $sql, $ns;
    
    $sql->db_Select($e107shop_db_name['orders'], "*", "order_number='".$order."' Group by order_number");
    $i=0;
    while($row=$sql->db_Fetch())
    {
        
            $top_order[$i][0] = $row["lineID"];
            $top_order[$i][1] = $row["cart_ID"];
            $top_order[$i][2] = $row["productID"];
            $top_order[$i][3] = $row["quantity"];
            $top_order[$i][4] = $row["option"];
            $top_order[$i][5] = $row["order_number"];
            $top_order[$i][6] = $row["status"];
            $top_order[$i][7] = $row["last_mod"];
            $top_order[$i][8] = $row["gateway"];
            $top_order[$i][9] = $row["theuser"];
            $top_order[$i][10] = $row["cust_region"];
            $top_order[$i][11] = $row["coupon"];
            $top_order[$i][12] = $row["sale_price"];
            $i++;
    }

    return $top_order;
    
}

/*
    Function : e107shop_admin_process_orders
    -----------------------------------------
    Parameters : $order     The orderid
    =========================================
    Updates the order with the payment-status
    =========================================
*/

function e107shop_admin_process_order($order, $completed, $paid) {
    global $e107shop_db_name, $sql, $ns, $finished;
    if (isset($paid) && isset($completed) && $finished == FALSE) {
        $statusnow = lan_admin_order_status_complete;
        e107shop_pay_order($order);
        $sql->db_Update($e107shop_db_name['orders'], "status='complete', last_mod='".e107shop_get_dateformat()."' where order_number='".$order."'");
    } else if (isset($paid) && $finished == FALSE) {
        $statusnow = lan_admin_order_status_paid;
        e107shop_pay_order($order);
    } else if (isset($completed) && $finished == TRUE) {
        $statusnow = lan_admin_order_status_complete;
        $sql->db_Update($e107shop_db_name['orders'], "status='complete', last_mod='".e107shop_get_dateformat()."' where order_number='".$order."'");
    }
    $ns->tablerender("<center>".lan_global_status."</center>", 
    "<center><br />".lan_admin_order_status_updated." ".$statusnow."</center>");
}

/*
    Function : e107shop_delete_order
    --------------------------------------
    Parameters : $order     The orderid
    ======================================
    Deletes any entry with an order_number
    that matches the $order variable
    ======================================
*/

function e107shop_delete_order($order) {
    global $e107shop_db_name, $sql, $ns;

    if($sql->db_Delete($e107shop_db_name['orders'], "order_number='".$order."'"))
    {
        $ns->tablerender("<center>".lan_global_status."</center>", 
        "<center><br />".lan_admin_order_status_deleted."</center>");
    }
    else
    {
        $ns->tablerender("<center>".lan_global_status."</center>", 
        "<center><br />".lan_admin_order_status_error."</center>");
    }
}
